<?php

include_once("validador.class.php");

class ID3
{

    protected $id;
    protected $file_name;
    protected $artist;
    protected $title;
    protected $track;
    protected $year;
    protected $album;
    protected $file;
    protected $image;
    protected $public;
    protected $userid;

    public function __construct($id, $artist, $title, $track, $year, $album, $file, $userid, $public = 0) {
        $this->id = $id;

        $validador = new Validador();

        $this->artist = $validador->valida("artist", $artist, "/[[:alnum:]]{1,50}/");
            if ($this->artist != $artist) throw new Exception($this->artist);
        $this->title = $validador->valida("title", $title, "/[[:alnum:]]{1,50}/");
	    if ($this->title != $title) throw new Exception($this->title);
        $this->track = $validador->valida("track", $track, "/[0-9]{1,2}/");
	    if ($this->track != $track) throw new Exception($this->track);
        $this->year = $validador->valida("year", $year, "/[0-9]{4}/");
	    if ($this->year != $year) throw new Exception($this->year);
        $this->album = $validador->valida("album", $album, "/[[:alnum:]]{1,50}/");
	    if ($this->album != $album) throw new Exception($this->album);
        $this->file = $validador->valida("file", $file, "/[[:alnum:]]+.[mp3|wav|ogg|wma]$/");
	    if ($this->file != $file) throw new Exception($this->file);
            
        $this->set_image_song();
        $this->userid = $userid;
        $this->public = $public;
        $this->file_name = "(".$this->year.")".$this->track.".".$this->title." - ".$this->artist."[".$this->album."]".substr($file,strlen($file)-4);
    }

//-----------------------------------------
// Pintar
//-----------------------------------------

    public function write_data() {
        echo  '</br>' .
             '--------------------------------------------------' . '</br>';
        echo '           DADES INTRODUIDES                      ' . '</br>';
        echo '--------------------------------------------------' . '</br>';
        echo 'File Name: ' . $this->file_name . '</br>';
        echo 'Artist: ' . $this->artist . '</br>';
        echo 'Title: ' . $this->title . '</br>';
        echo 'Track: ' . $this->track . '</br>';
        echo 'Year: ' . $this->year . '</br>';
        echo 'Album: ' . $this->album . '</br>';
        echo 'File: ' . $this->file . '</br>';
        echo 'Image: <img src="' . $this->image . '" width="40" height="40" alt="'. $this->file_name .'"></br>';
        echo '--------------------------------------------------' . '</br>';
    }

//-----------------------------------------
// Cookie
//-----------------------------------------

    public function search_by_author($autor) {
        echo "Files by Author: " . $autor . '</br>';
        foreach($_SESSION as $valor) {
            if ($valor->artist == $autor) echo $valor->file_name . '</br>';
        }
    }

    function add_cookie() {
        if (!isset($_SESSION[$this->file_name])) {
            $_SESSION[$this->file_name] = $this;
        }
    }

    public function write_data_cookie() {
        echo '--------------------------------------------------' . '</br>';
        echo '           DADES COOKIE                           ' . '</br>';
        echo '--------------------------------------------------' . '</br>';
        foreach($_SESSION as $valor) {
            echo '</br>';
            echo 'File Name: ' . $valor->file_name . '</br>';
            echo 'Artist: ' . $valor->artist . '</br>';
            echo 'Title: ' . $valor->title . '</br>';
            echo 'Track: ' . $valor->track . '</br>';
            echo 'Year: ' . $valor->year . '</br>';
            echo 'Album: ' . $valor->album . '</br>';
            echo 'File: ' . $valor->file . '</br>';
            echo 'Image: ' . $this->image . '</br>';
        }
        echo '--------------------------------------------------' . '</br>';
    }

//------------------------------------------------
// Search in the Web Service the image of the song
//------------------------------------------------

    private function set_image_song() {
	$base = "http://musicbrainz.org/ws/2/release?query=";
	$url = $base."release:".urlencode($this->title);
	$response = new DOMDocument();
	$response->load($url);

	$asins = array ();
	$this->image = "./Images/interrogante.png";
	foreach ($response->getElementsByTagname('asin') as $asin) {
	    $asins = $asin->parentNode->getElementsByTagname('name')->item(0);
	    if ($asins->textContent == $this->artist) {
		$baseImageAmazon = "http://images.amazon.com/images/P/ASIN/";
		$urlImageAmazon = $baseImageAmazon.$asin->textContent;
		$tam = getimagesize($urlImageAmazon);
		if ($tam[0] > 1) {
		    $this->image = $urlImageAmazon;
		    break;
		}
	    }
	}
    }

//-----------------------------------------
// Getters y Setters
//-----------------------------------------

    public function get_id() {
	return $this->id;
    }

    public function set_id($id) {
	$this->id = $id;
    }

    public function get_file_name() {
        return $this->file_name;
    }

    public function get_artist() {
        return $this->artist;
    }

    public function get_title() {
        return $this->title;
    }

    public function get_track() {
        return $this->track;
    }

    public function get_year() {
        return $this->year;
    }

    public function get_album() {
        return $this->album;
    }

    public function get_file() {
        return $this->file;
    }

    public function get_image() {
        return $this->image;
    }
    
    public function get_public() {
        return $this->public;
    }
    
    public function set_public($public) {
        $this->public = $public;
    }
    
    public function get_userid() {
        return $this->userid;
    }
    
//-----------------------------------------
// Other functions
//-----------------------------------------
    
    public function convert_to_array() {
        return array('id' => $this->id, 'image' => $this->image, 
            'artist' => $this->artist, 'title' => $this->title, 
            'track' => $this->track, 'year' => $this->year, 
            'album' => $this->album, 'public' => $this->public);
    }

    public function upload_song($tmp) {
	move_uploaded_file($tmp, ".\Musica\\".$this->file_name);
    }
    
}

?>
